<?php
/**
 * 功能描述：	表字段管理
 * @datetime	2022-07-11
 * @version   	v2.3.0
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Admin_FieldModel extends Model {

    /**
     * Notes:字段列表
     * User: ZHL
     */
    function getFieldList($mouldCode, $sortFlag, $tableId){
        $sql['field'] = '*';
        $sql['table'] = 'sysfield';
        $sql['where']['sortflag'] = $sortFlag;
        $sql['where']['tableid'] = $tableId;
        if($mouldCode){
            $sql['where']['mould_code'] = $mouldCode;
        }
        $sql['order'] = 'viewseq desc,id asc,createdate asc';
        return $this->conn('mysql')->select($sql);
    }

    /**
     * Notes:字段是否存在
     * User: ZhuHaili
     */
    function getTableField($table, $field){
        $db = $this->conn('mysql');
        $table = $db->db_fix.$table;
        $sql = "select count(*) counts from information_schema.columns ";
        $sql .= " where table_name = '$table' and column_name = '$field' and table_schema='$db->db_name'";
        return $db->query($sql, 1);
    }

    /**
     * Notes:字段详情
     * User: ZHL
     */
    function getFieldInfo($id){
        $sql['field'] = '*';
        $sql['table'] = 'sysfield';
        $sql['where']['id'] = $id;
        return $this->conn('mysql')->find($sql);
    }

    /**
     * Notes:字段明细详情
     * User: ZHL
     */
    function getFieldDetailInfo($fid){
        $sql['field'] = '*';
        $sql['table'] = 'sysfield_detail';
        $sql['where']['f_id'] = $fid;
        return $this->conn('mysql')->select($sql);
    }

    /**
     * Notes:获取菜单下开放的字段
     * User: ZHL
     */
    function getOpenClassFieldsInfos($menuId, $sortFlag, $tableId){
        $field = 'a.*,b.fielddesc,b.fieldclassrule';
        $sql['field'] = $field;
        $sql['table'] = 'sysfield';
        $sql['as'] = 'a';
        $sql['leftjoin']['sysfield_detail'] = 'b on a.id=b.f_id';
        $sql['where']['b.menu_id'] = $menuId;
        $sql['where']['sortflag'] = $sortFlag;
        $sql['where']['tableid'] = $tableId;
        $sql['where']['status'] = '1';
        $sql['order'] = 'a.viewseq desc,a.id asc,a.createdate asc';
        return $this->conn('mysql')->select($sql);
    }
}